package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;
import media.sound.SoundBackground;
import media.sound.SoundEffect;
import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class AskForSound extends State {
	private final int MENU_ITEM_YES = 0;
	private final int MENU_ITEM_NO = MENU_ITEM_YES + 1;
	private final int MENU_ITEM_BEGIN = MENU_ITEM_YES;
	private final int MENU_ITEM_END = MENU_ITEM_NO;
	private int sNumMenuItem = 2;

	private Menu m_askForSound;
	private int m_maxOfItem = 2;
	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private int selected;

	private int[] m_iMenuItemTex = new int[sNumMenuItem];
	private int[] m_iMenutemSelectedTex = new int[sNumMenuItem];

	private Texture m_texture = new Texture();

	private short m_iLoadingCount = 0;
	private boolean m_isLoading = true;

	@Override
	public void Init() {
		Loading.SetNumberPart(4);
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		selected = MENU_ITEM_BEGIN;
//		Device.DisableScaleFactor();

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		float w = m_fSrcWidth / 2;
		float h = m_fSrcHeight / 2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		// Load menu texture
		// Yes
		m_iMenuItemTex[MENU_ITEM_YES] = m_texture
				.initTexture(R.drawable.mnu_yes);
		m_iMenutemSelectedTex[MENU_ITEM_YES] = m_texture
				.initTexture(R.drawable.mnu_yes_select);
		// No
		m_iMenuItemTex[MENU_ITEM_NO] = m_texture.initTexture(R.drawable.mnu_no);
		m_iMenutemSelectedTex[MENU_ITEM_NO] = m_texture
				.initTexture(R.drawable.mnu_no_select);

		m_askForSound = new Menu(gl, m_maxOfItem,
				(int) Device.MENU_BUTTON_WIDTH, (int) Device.MENU_BUTTON_HEIGHT);
		m_askForSound.SetUseBgColor(true);
		m_askForSound.SetBackgroundColor(0, 0, 0);
		m_askForSound.SetEnableScaleItem(false);
		m_askForSound.SetEnableScaleTitle(false);

		m_askForSound.AddMenuItem(m_iMenuItemTex[MENU_ITEM_YES],
				m_iMenutemSelectedTex[MENU_ITEM_YES]);
		m_askForSound.AddMenuItem(m_iMenuItemTex[MENU_ITEM_NO],
				m_iMenutemSelectedTex[MENU_ITEM_NO]);
		m_texture.initTexture(R.drawable.mnu_afs_title);
		m_askForSound.SetTitle(m_texture.getTextureID(),
				Size.MNU_AFS_TITLE_WIDTH, Size.MNU_AFS_TITLE_HEIGHT);
		m_askForSound.SetSelectedMenuItem(selected);
//		Device.EnableScaleFactor();
	}

	public void LoadingResources() {
		switch (m_iLoadingCount){
		case 0:
			GameSound.SetBgSound(new SoundBackground(Device.GetContext()));
			break;
		case 1:
			GameSound.SetEffSound(new SoundEffect(Device.GetContext()));
			break;
		case 2:
			GameSound.GetEffSound().LoadMenuSelectSound();
			break;
		case 3:
			GameSound.GetEffSound().LoadChangeMenuSound();
			break;
		default:
				m_isLoading = false;
		}
		Loading.UpdateProgess();
		m_iLoadingCount++;
	}

	@Override
	public void Update(GamePlay gamePlay) {
		if (m_isLoading) {
			LoadingResources();
			return;
		}
		super.Update(gamePlay);
		
		System.out.println("ntScr = "
				+ (Size.MNU_AFS_TITLE_WIDTH * Device.GetScaleFactorW()) + " "
				+ (Size.MNU_AFS_TITLE_HEIGHT * Device.GetScaleFactorH()));
		

		
	}

	@Override
	public void Render() {
		if (m_isLoading)
			return;

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		float w = m_fSrcWidth / 2;
		float h = m_fSrcHeight / 2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();

		m_askForSound.Render();
	}

	@Override
	public void Destroy() {
		m_askForSound.Clear();
		m_askForSound = null;
		m_iMenuItemTex = null;
		m_iMenutemSelectedTex = null;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if (KeyBoard.IsKeyPressed(DK_PAD_DOWN)) {
			selected++;
			if (selected > MENU_ITEM_END)
				selected = MENU_ITEM_END;
			m_askForSound.SetSelectedMenuItem(selected);
		} else if (KeyBoard.IsKeyPressed(DK_PAD_UP)) {
			selected--;
			if (selected < MENU_ITEM_BEGIN)
				selected = MENU_ITEM_BEGIN;
			m_askForSound.SetSelectedMenuItem(selected);
		} else if (KeyBoard.IsKeyPressed(DK_PAD_MIDDLE)) {
			switch (selected) {
			case MENU_ITEM_YES:
				GameOption.isMusicOn = true;
				GameOption.isSoundOn = true;
				break;
			case MENU_ITEM_NO:
				GameOption.isMusicOn = false;
				GameOption.isSoundOn = false;
				break;
			}
			gamePlay.SwitchState(GameState.Splash);
		}
	}

}
